package com.mahiru.mahiruaiagent.rag;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

/**
 * 基于阿里云知识云服务增强顾问
 *
 * @Author Enkidu
 * @Date 2025/7/5 17:54
 */
@Slf4j
// @Component
public class ChatAppRagCloudAdvisorConfig {

    @Value("${spring.ai.dashscope.api-key}")
    private String dashscopeApiKey;

    @Bean
    public Advisor chatAppRagCloudAdvisor() {
        // 先拿api
        DashScopeApi dashScopeApi = new DashScopeApi(dashscopeApiKey);
        final String KNOWLEDGE_INDEX_NAME = "编程学习助手";
        // 配置检索增强器
        DocumentRetriever dashScopeDocumentRetriever = new DashScopeDocumentRetriever(dashScopeApi
                , DashScopeDocumentRetrieverOptions
                .builder()
                .withIndexName(KNOWLEDGE_INDEX_NAME)
                .build());
        // 创建顾问 指定检索增强器
        return RetrievalAugmentationAdvisor.builder().documentRetriever(dashScopeDocumentRetriever).build();
    }
}
